package cn.iocoder.yudao.module.erp.controller.admin.business.order.ordermethod;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.ordermethod.vo.OrderMethodPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.ordermethod.vo.OrderMethodRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.ordermethod.vo.OrderMethodSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.order.ordermethod.OrderMethodDO;
import cn.iocoder.yudao.module.erp.service.business.order.ordermethod.OrderMethodService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - 结算方式")
@RestController
@RequestMapping("/erp/order-method")
@Validated
public class OrderMethodController {

    @Resource
    private OrderMethodService orderMethodService;

    @PostMapping("/create")
    @Operation(summary = "创建结算方式")
    @PreAuthorize("@ss.hasPermission('erp:order-method:create')")
    public CommonResult<Long> createOrderMethod(@Valid @RequestBody OrderMethodSaveReqVO createReqVO) {
        return success(orderMethodService.createOrderMethod(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新结算方式")
    @PreAuthorize("@ss.hasPermission('erp:order-method:update')")
    public CommonResult<Boolean> updateOrderMethod(@Valid @RequestBody OrderMethodSaveReqVO updateReqVO) {
        orderMethodService.updateOrderMethod(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除结算方式")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:order-method:delete')")
    public CommonResult<Boolean> deleteOrderMethod(@RequestParam("id") Long id) {
        orderMethodService.deleteOrderMethod(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得结算方式")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:order-method:query')")
    public CommonResult<OrderMethodRespVO> getOrderMethod(@RequestParam("id") Long id) {
        OrderMethodDO orderMethod = orderMethodService.getOrderMethod(id);
        return success(BeanUtils.toBean(orderMethod, OrderMethodRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得结算方式分页")
    @PreAuthorize("@ss.hasPermission('erp:order-method:query')")
    public CommonResult<PageResult<OrderMethodRespVO>> getOrderMethodPage(@Valid OrderMethodPageReqVO pageReqVO) {
        PageResult<OrderMethodDO> pageResult = orderMethodService.getOrderMethodPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderMethodRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获取结算方式精简列表")
    @PreAuthorize("@ss.hasPermission('erp:order-method:query')")
    public CommonResult<List<OrderMethodRespVO>> getSimpleOrderMethodList() {
        OrderMethodPageReqVO orderMethodPageReqVO = new OrderMethodPageReqVO();
        orderMethodPageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<OrderMethodDO> orderMethodPage = orderMethodService.getOrderMethodPage(orderMethodPageReqVO);
        return success(convertList(orderMethodPage.getList(), item -> new OrderMethodRespVO().setId(item.getId())
                .setName(item.getName())));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出结算方式 Excel")
    @PreAuthorize("@ss.hasPermission('erp:order-method:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderMethodExcel(@Valid OrderMethodPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderMethodDO> list = orderMethodService.getOrderMethodPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "结算方式.xls", "数据", OrderMethodRespVO.class,
                        BeanUtils.toBean(list, OrderMethodRespVO.class));
    }

}